<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/static/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/wangEditor/release/wangEditor-fullscreen.css">
</head>
<style>
    input[disabled] {
        background: #d4d4d4;
    }
</style>
<body>
<div class="layui-form" lay-filter="layuiadmin-app-form-list" id="layuiadmin-app-form-list" style="padding: 20px 30px 0 0;">
    <input type="hidden" name="id" value="{$find.id}">
    <input type="hidden" name="pid" value="">
    <div class="layui-form-item">
        <label class="layui-form-label">当前优惠券</label>
        <div class="layui-input-inline">
            <input type="text" disabled value="{$find.name}" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">用户选择</label>
        <div class="layui-input-inline" style="width: 400px;">
            <input type="text" class="layui-input" ts-selected="" id="demo" lay-verify="required" placeholder="请选择用户" autocomplete="off">
        </div>
    </div>
    <script type="text/html" id="time">
        {{ formatDateTime(d.time) }}
    </script>
    <div class="layui-form-item layui-hide">
        <input type="button" lay-submit lay-filter="layuiadmin-app-form-submit" id="layuiadmin-app-form-submit" value="提交">
        <input type="button" lay-submit lay-filter="layuiadmin-app-form-edit" id="layuiadmin-app-form-edit" value="修改">
    </div>
</div>
<script src="/static/layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: '/static/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'laydate', 'form', 'tableSelect', 'upload', 'colorpicker'], function(){
        var $ = layui.$,
                form = layui.form,
                laydate = layui.laydate,
                upload = layui.upload,
                tableSelect = layui.tableSelect,
                colorpicker = layui.colorpicker;

        //表单赋值
        colorpicker.render({
            elem: '#test-form'
            ,color: '#ffffff'
            ,done: function(color){
                $('#test-form-input').val(color);
            }
        });

        //普通图片上传
        upload.render({
            elem: '#up'
            ,url: "{:url('Activity/upload')}"
            ,done: function(res){
                //如果上传失败
                if(res.code > 0){
                    return layer.msg('上传失败');
                }
                //上传成功
                layer.msg('上传成功');
                $("input[name=img]").val(res.img);
            }
        });

        tableSelect.render({
            elem: '#demo',	//定义输入框input对象
            checkedKey: 'id', //表格的唯一建值，非常重要，影响到选中状态 必填
            searchKey: 'info',	//搜索输入框的name值 默认keyword
            searchPlaceholder: '搜索商品名称',	//搜索输入框的提示文字 默认关键词搜索
            table: {	//定义表格参数，与LAYUI的TABLE模块一致，只是无需再定义表格elem
                url: "{:url('User/index')}",
                method: 'post',
                cols: [[
                    {type: 'checkbox'},
                    {field: 'name', title: '商品名称', width: 400},
                    {field: 'time', title: '加入时间', width: 200, templet: "#time"}
                ]]
            },
            done: function (elem, data) {
                //选择完后的回调，包含2个返回值 elem:返回之前input对象；data:表格返回的选中的数据 []
                //拿到data[]后 就按照业务需求做想做的事情啦~比如加个隐藏域放ID...
                var field = data.data;
                var pid = [];
                var name = [];
                for (var i = 0; i < field.length; i++){
                    pid.push(field[i].id);
                    name.push(field[i].name);
                }
                $("input[name=pid]").val(pid.join(','));
                $("#demo").val(name.join(','));
            }
        });

        //监听提交
        form.on('submit(layuiadmin-app-form-edit)', function(data){
            var field = data.field; //获取提交的字段
            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
            //提交 Ajax 成功后，关闭当前弹层并重载表格
            $.ajax({
                type: "POST",
                url: "{:url('Product/coupon_song')}",
                data: {field:field},
                dataType: "json",
                success: function(e){
                    if (e == 1){
                        layer.msg('发送成功', {offset: ['30%', '40%']});
                        setTimeout(function () {
                            parent.layui.table.reload('demo'); //重载表格
                            parent.layer.close(index); //再执行关闭
                        }, 500)
                    }else{
                        layer.msg('发送失败', {offset: ['30%', '40%']});
                    }
                }
            });
        });
    })

    //php时间戳用js转换为yyyy-mm-dd
    function formatDateTime(timeStamp) {
        var date = new Date();
        date.setTime(timeStamp * 1000);
        var y = date.getFullYear();
        var m = date.getMonth() + 1;
        m = m < 10 ? ('0' + m) : m;
        var d = date.getDate();
        d = d < 10 ? ('0' + d) : d;
        var h = date.getHours();
        h = h < 10 ? ('0' + h) : h;
        var minute = date.getMinutes();
        var second = date.getSeconds();
        minute = minute < 10 ? ('0' + minute) : minute;
        second = second < 10 ? ('0' + second) : second;
        return y + '-' + m + '-' + d+' '+h+':'+minute+':'+second;
    }

    //补零
    function returnFloat(value){
        value = Math.round(parseFloat(value)*100)/100;
        var xsd = value.toString().split(".");
        if(xsd.length == 1){
            value = value.toString()+".00";
            return value;
        }
        if(xsd.length>1){
            if(xsd[1].length<2){
                value = value.toString()+"0";
            }
            return value;
        }
    }
</script>
</body>
</html>